import { useSpinnerCountDown } from './components/useSpinnerCountDown'
import * as PIXI from 'pixi.js'
export const DiceGame = class DiceGame {
    app = null
    containers = null
    initGame(view) {
        const DPR = window.devicePixelRatio;
        this.app = new PIXI.Application({
            view,
            width: 375,
            height: 500,
            backgroundAlpha: 1, // default: 1 值从 0（完全透明）到 1（完全不透明）
            resolution: DPR,       // default: 1 分辨率
            antialias: true,    // default: false 抗锯齿
            autoDensity: true,
        })
        this.containers = {
            diceCountdownBox: new PIXI.Container({
                width: 204,
                height: 174
            }),
        }
        this.cb = useSpinnerCountDown({
            position: [100, 100],
            innerColor: 0x363D6F,
            outerStartColor: 0x329FEF,
            outerEndColor: 0x7CFAE5,
            innerRadius: 58,
            outerRadius: 65,
            parent: this.containers.diceCountdownBox,
            maxTime: 60,
            tips: 'Shaking'
        })

        let loadDiceAnimateArray = [];
        const loader = new PIXI.Loader();
        loader.add('imgs/dice_load.png').load(() => {
            for (let i = 0; i < 81; i++) {
                let texture = PIXI.utils.TextureCache['imgs/dice_load.png'];
                let rectangle = new PIXI.Rectangle(i * 80, 0, 80, 80); // x,y,w,h
                texture.frame = rectangle;
                let rocket = new PIXI.Sprite(texture);
                rocket.width = 40;
                rocket.height = 40;
                loadDiceAnimateArray.push(rocket);
            }
            loadDiceAnimateArray.forEach(item => {
                item.x = Math.random() *300
                item.y = Math.random() *300
                this.app.stage.addChild(item)
            })
            let loadDiceAnimate = new PIXI.AnimatedSprite(loadDiceAnimateArray);
            loadDiceAnimate.width = 40;
            loadDiceAnimate.height = 40;
            this.containers.diceCountdownBox.addChild(loadDiceAnimate);
        });
        
        
        
        // ackground: url(/src/assets/imgs/dice_load.png) 0 0 / 3240px 40px no-repeat;
        this.app.renderer.backgroundColor = 0x191c35;
        
        this.app.stage.addChild(this.containers.diceCountdownBox);

        this.app.ticker.add((delta) => {
            this.cb(delta)
        });
    }
}